<?php

/**
* 计算公式辅助类
*/

class CalHelper {

	/** BOSS角色初始化 $t代表目标人，$r是数据库资源，用于获取卡牌资料**/
	public static function boss_init($t,$r){
		$t->gender = $r['gender'];
		$t->element = $r['element'];
		$t->isboss = 1;
		$namearr=explode(":",$t->name);
		$t->name=$namearr[0];

		//等级判定
		if(!$namearr[1] || !is_numeric($namearr[1])) {
			$t->baselv = $t->level = 99;
		} else {
			$t->baselv = $t->level = $namearr[1];
		}

		//BOSS名称判定
		if(is_numeric(substr($t->name,-1))) {
			$t->againcount = substr($t->name,-1);
			$t->name = substr($t->name,4,(strlen($t->name)-5));
		} else {
			$t->againcount = 1;
			$t->name = substr($t->name,4);
		}

		//行动回数判定
		if($t->againcount > 1) {
			$t->atkafter_status[11]->on = 1;
			$t->atkafter_status[11]->time = $t->againcount;
		}
	
		//启用宝箱掉落开关
		$t->defafter_status[9]->on = 1;

		$_SESSION['extrainfo']['boss_fight'] = 1;
	}

	/** 回合前进判断函数。$mark 默认为0，表示正常的流程，设为1表示强制前进回合，不作行动点判断。*/
	public static function go_turn($mark=0) {

		$c = array_merge($_SESSION['a'],$_SESSION['b']);
		
		if($mark == 0) {
			foreach($c as $t) {
				$t->standing = 0;
				if($t->hp <= 0) {
					StatusHelper::resetall($t);
					$t->done = 1;
				}
				if($t->escaping == 1) $t->done = 1;
				if($t->done <= 0)
					return 0;
			}
		}
	
		$_SESSION['show'] .= "Turn ".$_SESSION['turn']."结束。<br>";
		$_SESSION['turn'] += 1;
/*
		if(($_SESSION['turn']-1) % 3 == 0) {
			self::terrain_cal(0);
			$_SESSION['result'] .= "<font color=blue>三回合经过，地形已重新生成。</font><br>";
		}
*/
		$orderps = array();
		foreach($c as $t) {
			$t->done = 0;
			$t->agained = 0;
			//回合制状态步进
			foreach($t->turnskill_status as $tt) {
				if($tt->on == 1) $tt->run_turn($t);
			}
			$orderps[$t->name] = $t->spd;
			if($t->hp <= 0) continue;
			$recoverap = 4;
			if($_SESSION['T'] == 4) {	//罗马斗技场
				$recoverap += 1;
			}
			if($t->recoverap) $recoverap += 2;
			if(self::ifgroupskill($t) == 4 && StatusHelper::has_abn($t) && $t->hp > 0) {
				$_SESSION['show'] .= $t->name."发动覇王の盟約，自回复AP增加<br>";
				$recoverap += $_SESSION['turn'];
			}
			if(self::ifgroupskill($t) == 18 && $t->groupskcount < 5 && $t->hp > 0) {
				if($t->groupskcount > 0) {
					$_SESSION['show'] .= $t->name."的SP缓慢恢复中...第".($t->groupskcount+1)."回合<br>";
					$healsp = round($t->mxsp * 0.05);
					$t->sp = ($t->sp + $healsp > $t->mxsp) ? $t->mxsp : ($t->sp + $healsp);
					$t->groupskcount += 1;
				} elseif($t->hp < round($t->mxhp/2) && $t->groupskcount == 0 && $t->hp > 0) {
					$_SESSION['show'] .= $t->name."发动堕天の烙印！SP自行恢复中...<br>";
					$healsp = round($t->mxsp * 0.05);
					$t->sp = ($t->sp + $healsp > $t->mxsp) ? $t->mxsp : ($t->sp + $healsp);
					$t->groupskcount += 1;
				}
			}
			if(self::ifgroupskill($t) == 19 && $t->groupskcount < 5 && $t->hp > 0) {
				if($t->groupskcount > 0) {
					$_SESSION['show'] .= $t->name."的HP缓慢恢复中...第".($t->groupskcount+1)."回合<br>";
					$healhp = round($t->mxhp * 0.06);
					$t->hp = ($t->hp + $healhp > $t->mxhp) ? $t->mxhp : ($t->hp + $healhp);
					$t->groupskcount += 1;
				} elseif($t->hp < round($t->mxhp/3) && $t->groupskcount == 0 && $t->hp > 0) {
					$_SESSION['show'] .= $t->name."发动天使の聖痕！HP自行恢复中...<br>";
					$healhp = round($t->mxhp * 0.06);
					$t->hp = ($t->hp + $healhp > $t->mxhp) ? $t->mxhp : ($t->hp + $healhp);
					$t->groupskcount += 1;
				}
			}
			if($t->before_status[75]->on == 1) {
				$recoverap = floor($recoverap/2);
			}
			self::apointchange_2($t,$recoverap);
			$t->ap = self::apointchange($t->ap,2);
			$extra_sap = self::cal_extra_getsap($t);
			if($extra_sap > 0) {
				$_SESSION['show'] .= $t->name."的底力逐渐爆发中，额外获得".$extra_sap."SAP，剩余底力".$t->sapool."SAP<br>";
				$t->sap = self::apointchange($t->sap,$extra_sap);
			}
		}
		$_SESSION['show'] .= self::get_turn_apstat();
		$_SESSION['extrainfo']['orderlist'] = get_order($orderps,3);

	}

	/** 生成回合报告文本 **/
	public function get_turn_apstat() {
		$r = '<p>本回合AP结算报告:<br>';
		$r .= '<p>A方:<br>';
		foreach($_SESSION['a'] as $sa)  {
			$r .= $sa->name."&nbsp;AP：".$sa->ap."&nbsp;SAP：".$sa->sap."<br>";
		}
		$r .= '------<br>B方:<br>';
		foreach($_SESSION['b'] as $sb)  {
			$r .= $sb->name."&nbsp;AP：".$sb->ap."&nbsp;SAP：".$sb->sap."<br>";
		}
		$r .= "<br>";
		return $r;
	}
	
	/** 计算额外SAP获得量，每减少5%的HP可以在获得SAP时额外获得1点SAP，合计最多可以额外获得20点 **/
	public function cal_extra_getsap($a) {
		$stephp = floor($a->mxhp * 0.05);
		$getsap = floor(($a->lasthp - $a->hp)/$stephp);
		if($getsap <= 0) return 0;
		if($a->sapool >= $getsap) {
			$r = $getsap;
		} else {
			$r = $a->sapool;
		}
		$a->sapool -= $r;
		$a->lasthp = $a->hp;
		if($a->sapool < 0) $a->sapool = 0;
		return $r;
	}

	/** 判断组别技能是否允许发动 $a 判断对象 **/
	public static function ifgroupskill($a) {
		if($_SESSION['extrainfo']['ooduel'] == 1) return -1;
		return $a->groupid;
	}

	/** AP变化包装函数 */
	public static function apointchange($ap,$value) {
		$ap += $value;
		if($ap < 0) {
			return 0;
		} elseif($ap > 50) {
			return 50;
		} else {
			return $ap;
		}
	}
	
	/** AP变化包装函数2 */
	public static function apointchange_2($a,$value) {
		$a->ap = self::apointchange($a->ap,$value);
		$a->sap = self::apointchange($a->sap,$value);
	}
	
	/** 
	*  计算消耗
	*/
	public static function cost_cal($a,$sk){
		if($a->hp < $sk->hp) {
			$_SESSION['show'] .= "剩余HP不足！<br>";
			return 0;
		}
		if($a->sp < $sk->sp) {
			$_SESSION['show'] .= "剩余SP不足！<br>";
			return 0;
		}
		
		if($sk->isunique == 1) {
			if($a->sap < $sk->ap) {
				$_SESSION['show'] .= "剩余SAP不足！<br>";
				return 0;
			}
		} else {
			if($a->ap < $sk->ap) {
				$_SESSION['show'] .= "剩余AP不足！<br>";
				return 0;
			}
		}
		
		
		$a->hp -= $sk->hp;
		$a->sp -= $sk->sp;
		if($sk->isunique == 1) {
			$a->sap = self::apointchange($a->sap,-1*($sk->ap));
		} else {
			$a->ap = self::apointchange($a->ap,-1*($sk->ap));
		}
		
		$_SESSION['result'] .= $a->name."&nbsp;HP消耗：".$sk->hp." SP消耗：".$sk->sp." AP消耗：".$sk->ap."<br>";
		
		return 1;		
	}

	/** 计算一组中的生存人数(排除逃跑的)
	    * $t:对象组 返回此组的生存人数，如果mark=1,则返回最少为1
	**/
	public static function live_count($t,$mark=1) {
		$sum = 0;
		foreach($t as $b) {
			if($b->hp > 0 && !$b->escaping) $sum += 1;
		}
		if($mark == 1) {
			return $sum=($sum == 0) ? 1 : $sum;
		} else {
			return $sum;
		}
	}

	/** 计算一组中的有效战斗人员(含死亡者) **/
	public static function fighter_count($t) {
		$sum = 0;
		foreach($t as $b) {
			if(!$b->escaping) $sum += 1;
		}
		return $sum;
	}

	
	/** 耐性倍率计算 
	 *  $b:防御方 $element:耐性计算类型
	 *  0物 1火 2冰 3雷 4地 5风 6光 7暗 8万能
	 *  返回：计算后的res倍率
	 *  0无效 0.6耐 1平 1.25弱 -1反 -2吸
	 **/
	public static function res_cal($b,$element,$sk) {

		switch ($b->res[$element]) {
			case 0: {return 1;}
			case 1: {
				if($sk->thru == 1) return 1;
				if($sk->atk_type == 8) return 0.6;
				if($sk->all_atk == 1) return 0.7;
				else return 0.6;
				}
			case 2: {
				if($sk->thru == 1) return 1;
				if($sk->all_atk == 1) return 0.7;
				else return 0;
				}
			case 3: {
				if($sk->all_atk == 1) return 0.7;
				else return -1;
				}
			case 4: {
				if($sk->all_atk == 1) return 0.7;
				else return -2;
				}
			case 5: {
				if($b->standing == 1) {	//对已经倒地的不能重复倒地
					return 1;
				} else {
					return 1.25;
				}
			}
		}
	}
	
	/** 判断是否有弱点
	 * $t:判断对象 $mark:为0则检查所有耐性，为1则排除光暗弱点检测
	 * 返回0说明无弱点，返回1说明有弱点
	**/
	public static function has_weakpoint($t,$mark) {
		switch($mark) {
		case 0:{
			for($i=0;$i<=7;$i++) {
				if($t->res[$i] == 5) return 1;
				continue;
			}
			break;
		}
		case 1:{
			for($i=0;$i<=5;$i++) {
				if($t->res[$i] == 5) return 1;
				continue;
			}
			break;
		}
		default:{}
		}
		return 0;
	}

	/** 反射/吸收形式计算
	 *  $a:攻击方 $b:防御方 $sk:技能 $element:攻击属性
	 *  
	 *  返回：0正常 1反射 2吸收 
	 *  
	 **/
	public static function ref_absorb_cal($a,$b,$sk,$element) {
		
		$ref_absorb = 0; //反射1,吸收2
		//耐性倍率

		$bres = self::res_cal($b,$element,$sk);
		$sk->res = $bres >= 0 ? $bres : 1;
		if($sk->backfire == 1) {
			$_SESSION['show'] .= "攻击反噬！<br>";
			$ref_absorb = 1;
		}	elseif($bres == -1 || $sk->ref == 1) {
			//$sk->res = self::res_cal($a,$element,$sk);				
			$_SESSION['show'] .= "攻击被反射！<br>";
			$ref_absorb = 1;
		}	elseif($bres == -2) {
			$_SESSION['show'] .= "攻击被吸收!<br>";
			$ref_absorb = 2;
		}
		
		return $ref_absorb;
	}

	/** 反射/吸收实际数值计算
	 *  $a：攻击方 $ref_absorb 反射1/吸收2 $sk：技能 $dmg：账面伤害
	**/
	public static function refabsorb_dmg_cal($a,$ref_absorb,$sk,$dmg) {
		if($ref_absorb == 2) return floor($dmg * -0.5);
		$ares = $a->res[$sk->atk_type];
		if($ares == 0 || $ares == 5) {	//弱平
			$dmg = floor($dmg * 0.75);
		} elseif($ares == 1) {	//耐
			$dmg = floor($dmg * 0.55);
		} elseif($ares == 2) {	//无
			$dmg = floor($dmg * 0.4);
		} else {	//反吸
			$dmg = floor($dmg * 0.3);
		}
		return $dmg;
	}

	//即死专用耐性计算
	public static function dead_res_cal($a,$b,$sk) {
		if(isset($sk->exdtype)) {
			$element = $sk->exdtype;
		} else {
			$element = $sk->dtype;
		}
		switch ($b->res[$element]) {
			case 0: {return 1;break;}
			case 1: {return 0.5;break;}
			case 2: {return 0;break;}
			case 3: {return -1;break;}
			case 4: {return 0;break;}
			case 5: {return 1.5;break;}
		}
		$sk->res = $result;
		return $result;
	}

	/** 每次的伤害数字结算
	 *  $a:攻击方 $b:防御方 $mark:标记，0为普通,1为反射,2为吸收 $dmg:伤害值
	 **/
	public static function apply_damage($a,$b,$mark,$dmg) {
		$b->hited = 1;
		switch($mark) {
		case 0:{
			$b->hp -= $dmg;
			StatusHelper::afterdamage_event($b);
			$a->makedmg += $dmg;
			break;
		}
		case 1:{
			$a->hp -= $dmg;
			StatusHelper::afterdamage_event($a);
			break;
		}
		case 2: {
			$b->hp += abs($dmg);
			if ($b->hp > $b->mxhp) {
				$b->hp = $b->mxhp;
			}
			break;
		}
		default: {}
		}
		return 1;
	}

	/** 攻击后的额外奖励AP结算
	 *  $a:攻击方 $b:防御方 0为普通,1为反射,2为吸收 $dmg:伤害值 $sk：技能对象
	 **/
	public static function apply_bonusap($a,$b,$ref_absorb,$dmg,$sk) {
		$res = $sk->res;
		if($ref_absorb == 0 && $dmg == 0 && $b->hited == 1 && !$sk->egg) {
			$_SESSION['show'] .= $b->name."无效化对手攻击，获得额外AP！<br>";
			self::apointchange_2($b,1);
		} elseif(($ref_absorb == 1 && $b->hited == 1) && $sk->ref == 0 && !$sk->backfire) {
			$_SESSION['show'] .= $b->name."反射对手攻击，获得额外AP！<br>";
			self::apointchange_2($b,1);
		} elseif($ref_absorb == 2 && $dmg < 0 && $b->hited == 1) {
			$_SESSION['show'] .= $b->name."吸收对手攻击，获得额外AP！<br>";
			self::apointchange_2($b,1);
		} else {
			return 0;
		}
		return 1;
	}

	/** 复合伤害因素判定 $a：技能使用者 $sk：技能对象 返回array(伤害因数1能力值,伤害因数2能力值) **/
	public static function multi_dmg_factor($a,$sk) {
		if(!$sk->dmgfactor || count($sk->dmgfactor) != 2) {
			if($sk->atk_type == 0) {
				return array($a->str,$a->str);
			} else {
				return array($a->mgr,$a->mgr);
			}
		}
		$dmgfactor = array();
		foreach($sk->dmgfactor as $df) {
			switch($df) {
			case 'str':{$dmgfactor[] = $a->str;break;}
			case 'mgr':{$dmgfactor[] = $a->mgr;break;}
			case 'spd':{$dmgfactor[] = $a->spd;break;}
			case 'vit':{$dmgfactor[] = $a->vit;break;}
			case 'luk':{$dmgfactor[] = $a->luk;break;}
			default:{$dmgfactor[] = $a->str;break;}	//不应该出现
			}
		}
		return $dmgfactor;
	}

	/** 复数次攻击伤害衰减比率计算 $mcount：攻击次数 **/
	public static function multi_decay($mcount) {
		switch($mcount) {
		case 1:{$plus = 0.9;break;}
		case 2:{$plus = 0.7;break;}
		case 3:{$plus = 0.55;break;}
		case 4:{$plus = 0.45;break;}
		default:{$plus = 0.4;}
		}
		return $plus;
	}

	/** 伤害等级倍率计算 $skpwr：技能实际伤害基数，$defabi：技能对应的防御主因数 **/
	public static function dmglvpwr_cal($skpwr,$defabi) {
		$dmglvpwr = 1;
		if($skpwr >= 170) {
			return $dmglvpwr;
		}
		$dmgy = round($defabi/2)-10;
		$dmgy = $dmgy < 0 ? 0 : $dmgy;
		$dmgy = $dmgy > 40 ? 40 : $dmgy;
		if($skpwr < 150) {
			$dmglvpwr = (100-$dmgy * 1.5)/100;	//普
		} elseif($skpwr < 160) {	
			$dmglvpwr = (100-$dmgy * 1.2)/100;	//小
		} elseif($skpwr < 170) {
			$dmglvpwr = (100-$dmgy * 0.8)/100;	//中
		}
		if($dmglvpwr <= 0) {
			$dmglvpwr = 1;
		}
		return $dmglvpwr;
	}

	/** 全体伤害减成倍率 $tgtnum：对象人数 **/
	public static function tgtnum_cal($tgtnum) {
		switch ($tgtnum) {
			case 0: $odds  = 1;break;
			case 1: $odds  = 0.95;break;
			case 2: $odds = 0.85;break;
			case 3: $odds = 0.75;break;
			case 4: $odds = 0.7;break;
			default: $odds = 0.7;
		}
		return $odds;
	}
	
	/** 物理伤害计算
	 * $a:攻击方 $b:防御方 $sk:技能 $element:伤害属性
	 * 物理伤害=伤害等级基数×（能力一Y×0.7+能力二Y×0.3+等级Y）%×[（攻击方力Y×3+等级Y）/（防御方耐Y×2+力Y+等级Y）]×各级物理伤害等级倍率×（破坏神+布斯塔）×全体倍率×
	 * 复数减成×异常or必杀修正×随机波动×耐性×大魔王×黑裙子×蓄力
	 * 返回：伤害值
	 */
	public static function phy_dmg_cal($a,$b,$sk,$element) {

		//全体/扩散主目标 减成
		$all = self::tgtnum_cal($sk->tgtnum);

		//在全体攻击这个被动技能下的普攻，没有攻击人数减成
		if ($sk->id == 2){
			$all = 1;
		}
		
		//防御倍率
		$def = 1;
		
		//DOWN补正
		$sk->down = isset($sk->down) ? $sk->down : 1;
		
		//魅惑激昂判断，喵的就这样了
		$charmorhigh = 1;
		if($a->before_status[26]->on == 1 && $a->before_status[26]->committer == $b->uuid) {
			$charmorhigh = 0.4;
		}

		if ($b->before_status[25]->on == 1) {
			$charmorhigh *= 1.5;
		}

		//额外加成，用于随伤势增加威力的技能等
		if(!$sk->expwr) $sk->expwr = 1;

		if(!$sk->charge) $sk->charge = 1;

		//破防处理，先道具后技能效果
		if($a->eventskill_status[4]->on == 1) {
			$a->eventskill_status[4]->action($sk,$a,$b);
		} elseif($b->defskill_status[3]->on == 1) {
			if($sk->defbreak != 1) {
				switch ($sk->res) {
				case 0.6: $def = 0.7;break;
				case 1:   $def = 0.7;break;
				case 1.25: $sk->res = 1;break;
				}
			} else {
				$_SESSION['show'] .= $a->name."破防效果发动！。<br>";
			}
		}

		//会心倍率
		$crt = self::crt_cal($a,$b,$sk);
		//浮动倍率
		$float = mt_rand($sk->minp,$sk->maxp)/100;
		//伤害等级倍率
		$dmglvpwr = self::dmglvpwr_cal($sk->pwr,$b->str);

		//可能的复合能力因子计算
		list($ability1,$ability2) = self::multi_dmg_factor($a,$sk);

		$dmg = $sk->pwr * ((round($ability1/2) * 0.7) + (round($ability2/2) * 0.3) + round($a->level/2))/100 * 
			(round($a->str/2) * 3 + round($a->level/2))/(round($b->vit/2) * 2 + round($b->str/2) + round($b->level/2)) *
			$dmglvpwr * (1 + $sk->destroyer + $sk->buste + $sk->phybuste) * $all * $crt * $float * $def * $charmorhigh *
			$sk->res * $sk->plus * (1 - $sk->lordg) * (1 - $sk->bdress) * $sk->expwr * $sk->charge * $sk->down;
		return floor($dmg);	
	}
	
	/** 魔法伤害计算
	 * $a:攻击方 $b:防御方 $sk:技能 $element:伤害属性
	 * 魔法伤害=伤害等级基数×（能力一Y×0.7+能力二Y×0.3+等级Y）%×[（攻击方魔Y×3+等级Y）/（防御方耐Y×2+魔Y+等级Y）]×各级魔法伤害等级倍率×（破坏神+布斯塔+海布斯塔+属性修正
	 * +光暗地型修正）×全体倍率×复数减成×异常修正×随机波动×耐性×大魔王×黑裙子×精神集中
	 * 返回：伤害值
	 */
	public static function mgc_dmg_cal($a,$b,$sk,$element) {
		
		//全体/扩散主目标 减成
		$all = self::tgtnum_cal($sk->tgtnum);
		
		//属性加成
		if($a->element == $element) {
			$sk->element = 0.05;
		} else {
			$sk->element = 0;
		}
			
		//防御倍率
		$def = 1;
		
		//DOWN补正
		$sk->down = isset($sk->down) ? $sk->down : 1;

		//额外加成，用于随伤势增加威力的技能
		if(!$sk->expwr) $sk->expwr = 1;
		
		//魅惑激昂判断，喵的就这样了
		$charmorhigh = 1;
		if($a->before_status[26]->on == 1 && $a->before_status[26]->committer == $b->uuid) {
			$charmorhigh = 0.4;
		} 

		if($b->before_status[25]->on == 1) {
			$charmorhigh *= 1.5;
		}

		//破防处理，先道具后技能效果
		if($b->defskill_status[3]->on == 1) {
			if($a->eventskill_status[4]->on == 1) {
				$a->eventskill_status[4]->action($sk,$a,$b);
			} elseif($sk->defbreak != 1) {
				switch ($sk->res) {
				case 0.6: $def = 0.7;break;
				case 1:   $def = 0.7;break;
				case 1.25: $sk->res = 1;break;
				}
			} else {
				$_SESSION['show'] .= $a->name."破防效果发动！。<br>";
			}
		} 

		//会心倍率
		$crt = self::crt_cal($a,$b,$sk);
		//浮动倍率
		$float = mt_rand($sk->minp,$sk->maxp)/100;
		//伤害等级倍率
		$dmglvpwr = self::dmglvpwr_cal($sk->pwr,$b->mgr);
		//纯万能伤害基数下降
		if($sk->atk_type == 8) {
			$sk->pwr = floor($sk->pwr * 0.9);
		}

		//可能的复合能力因子计算
		list($ability1,$ability2) = self::multi_dmg_factor($a,$sk);

		$dmg = $sk->pwr * ((round($ability1/2) * 0.7) + (round($ability2/2) * 0.3) + round($a->level/2))/100 * 
			(round($a->mgr/2) * 3 + round($a->level/2))/(round($b->vit/2) * 2 + round($b->mgr/2) + round($b->level/2)) *
			$dmglvpwr * (1 + $sk->destroyer + $sk->buste + $sk->hibuste + $sk->element + $sk->enhanced) * $all * $crt * $float * $def * $charmorhigh *
			$sk->res * $sk->plus * (1 - $sk->lordg) * (1 - $sk->bdress) * $sk->expwr * $sk->charge * $sk->down;
			    
		return round($dmg);	
	}
	
	/** 回复魔法计算
	 *  $a:行动方  $sk:技能，如果$sk->bonus=-1，则代表完全恢复
	 *  返回：回复值
	 */
	public static function cure_cal($a,$b,$sk) {
		//全体减成
		$all = self::tgtnum_cal($sk->tgtnum);

		if($sk->bonus == -1) {
			$hp = $b->mxhp - $b->hp;
		} else {
			$hp = $sk->pwr * ($a->mgr/2 + $a->level/2)/100 * $sk->plus * $all + $sk->bonus;
		}
		return round($hp);
	}
	
	/** 会心倍率计算
	 *  $a:攻击方 $b:防御方 $sk:技能
	 *  返回：会心倍率
	 */
	public static function crt_cal($a,$b,$sk) {
		if(!isset($sk->crtmaster)) $sk->crtmaster = 1.35;
		if(!isset($sk->crtbuff)) $sk->crtbuff = 0;
		if($sk->res == 1.25 || $b->defskill_status[0]->on == 1) return 1;
		if($sk->atk_type == 8) return 1;	//纯万能不会心
		$c = round(($sk->crt + round($a->luk/2-10) * 1.3)*(100 - (round($b->luk/2-10) * 2))/100) + $sk->crtbuff;
		$c = ($c < 0) ? 0 :$c;
		$_SESSION['result'] .= "会心率:".$c."%<br>";
		$b->crted = 0;
		if($c >= mt_rand(1,100)) { 
			$crt = $sk->crtmaster;
			$b->crted = 1; //代表b被会心命中。
			$_SESSION['show'] .= "<strong>会心一击！</strong>&nbsp;";
		}
		else {
			$crt = 1;
		}
		return $crt;
	}
	
	/** 伤害命中率计算
	 * $a:攻击方 $b:防御方 $sk:技能 $multi:多段攻击实际次数 如果$sk->hits = -1，表示必中
	 * 100*[（攻击方力Y×0.5+攻击方魔Y×0.5+攻击方速Y+攻击方运Y+等级Y）/（回避方速Y×2+回避方运Y+等级Y）]=攻方总命中率-其它各种技能补正-单复数补正=实际命中率
	 * 单数-1
	 * 复数-总HIT数*2
	 * 返回：命中率
	 */
	public static function hit_cal($a,$b,$sk,$multi = 0) {
		if($sk->hits == -1) {
			$hit = 100;
		} else {
			$hit = round((round($a->str/2) * 0.5 + round($a->mgr/2) * 0.5 + round($a->spd/2) + round($a->luk/2) + round($a->level/2))/
				(round($b->spd/2) * 2 + round($b->luk/2) + round($b->level/2)) * 100) + $sk->hits;
			$hit = ($multi == 0) ? ($hit - 1) : ($hit-$multi*2);
		}
		$_SESSION['result'] .= "命中率:".$hit."<br>";	
		return $hit;
	}

	/** 即死成功率计算函数
	  * 参数说明：$a $b 攻防双方；$res 即死耐性 $hit 额外加成 $dtype 即死属性 6=光 7=暗 8=万能 9=真万能(无视耐性)
	  * 如果$base<=0，则代表其绝对值为固定确率 返回值为成功率
	**/
	public static function dead_odds_cal($a,$b,$sk,$dtype) {
		if($b->isboss == 1) {
			$_SESSION['show'] .= "<font color=red>即死无效</font><br>";
			return 0;
		}
		//全体/扩散主目标 减成
		$all = self::tgtnum_cal($sk->tgtnum);
	
		$base = $sk->dbase;
		$hit = $sk->shits;

		$override = 1;
		if($b->before_status[50]->on == 1) {
			$override = 0.5;
		}

		if($base < 50) {
			$dead_avoid = round(100 - (round($b->luk/2)-10) * 2.4);	//低
		} elseif($base < 60) {
			$dead_avoid = 100 - (round($b->luk/2)-10) * 2;	//中
		} else {
			$dead_avoid = round(100 - (round($b->luk/2)-10) * 1.6);	//高
		}
		$dead_avoid = ($dead_avoid < 0) ? 0 : $dead_avoid;
		/** 
			即死基本成功率：基数(低20%中40%高60%)×(攻击方魔Y+等级Y)%+布斯塔加成+其他
			即死、异常基本成功率：基数（低40%中50%高60%）×（攻击方魔Y+等级Y）%+布斯塔加成+其他（沉淀空气）
			即死成功率：即死基本成功率×各级即死回避率×耐性×全体的倍率×检查or大检查
		*/
		if($base <= 0) {
			$result = (self::res_cal($b,$dtype,$sk) <= 0) ? 0 : abs($base*$override);
		} else {
			$deadres = self::dead_res_cal($a,$b,$sk);
			$result=floor(($base*(round($a->mgr/2)+round($a->level/2))/100+$hit)*$dead_avoid*$deadres*$all*(1-$b->analyze)*$override/100);
		}
		$_SESSION['result'] .= "即死成功率：".$result."<br>";
		return $result;
	}

	/** 光暗生还成功判断函数，$sk 技能对象 $t 被攻击方对象
	 *
	**/
	public static function alive_proc($sk,$t) {
		if($t->eventskill_status[23]->on == 1) { //テトラジャ最优先
			$_SESSION['show'] .= "テトラジャ发动，即死伤害被抵消<br>";
			$t->eventskill_status[23]->cancel($t);
			return 1;
		}
		if($t->defafter_status[3]->on == 1 || $t->defafter_status[4]->on == 1) return 0; //优先级低于不屈和硬食
		if($sk->dtype > 7 || $sk->exdtype > 7) return 0; //非光暗即死不发动生还
		if($t->eventskill_status[5]->on == 1 && $b->luk >= mt_rand(1,100)) {
			$_SESSION['show'] .= "光/暗生還发动，即死伤害被抵消<br>";
			$t->eventskill_status[5]->cancel($t);
			return 1;
		}
		if($t->eventskill_status[6]->on == 1) {
			$_SESSION['show'] .= "光/暗生還发动，即死伤害被抵消<br>";
			$t->eventskill_status[6]->cancel($t);
			return 1;
		}
		return 0;
	}

	/** 异常成功率计算函数
	  * 参数说明：$a $b 攻防双方；$base 异常成功基本概率；$hit 额外加成；$multi 全体人数
	  * 如果$base<0，则代表其绝对值为固定确率
	**/
	public static function abn_cal($a,$b,$base,$hit,$multi) {
		//全体/扩散主目标 减成
		$all = self::tgtnum_cal($multi);

		$override = 1;
		if($b->before_status[50]->on == 1) {
			$override = 0.5;
		}
		/** 
		 * 即死、异常基本成功率：基数（低40%中50%高60%）×（攻击方魔Y+等级Y）%+布斯塔加成+其他（沉淀空气）
		 * 异常成功率：异常基本成功率×各级异常回避率×全体的倍率×女神的心得×检查or大检查
		*/
		if($base < 50) {
			$abn_avoid = round(100 - (round($b->luk/2)-10) * 2);	//低
		} elseif($base < 60) {
			$abn_avoid = round(100 - (round($b->luk/2)-10) * 1.8);	//中
		} else {
			$abn_avoid = round(100 - (round($b->luk/2)-10) * 1.5);	//高
		}
		$abn_avoid = ($abn_avoid < 0) ? 0 : $abn_avoid;

		if($base < 0) {
			return $result=abs($base*$override);
		} else {
			return $result=round(($base*($a->mgr/2+$a->level/2)/100+$hit)*$abn_avoid*$all*(1-$b->analyze)*$override/100);
		}
	}


	/** 判断是否是1v1状态，返回0表示否，返回1表示是 **/
	public static function isonetone() {
		if(self::live_count($_SESSION['a']) == 1 && self::live_count($_SESSION['b']) == 1) {
			return 1;
		}
		return 0;
	}

	/** HP越少伤害越大技能的伤害倍率计算函数，$sk是技能对象，$a是人物对象 **/
	public static function hurt_power($sk,$a) {
		if($a->hp <= round($a->mxhp * 0.75) && $a->hp > round($a->mxhp * 0.5)) {
			$_SESSION['show'] .= $a->name."受轻伤，".$sk->name."威力略增大。<br>";
			return 1.25;
		} elseif($a->hp <= round($a->mxhp * 0.5) && $a->hp > round($a->mxhp * 0.25)) {
			$_SESSION['show'] .= $a->name."受伤了，".$sk->name."威力增大。<br>";
			return 1.45;
		} elseif($a->hp <= round($a->mxhp * 0.25) && $a->hp > round($a->mxhp * 0.1)) {
			$_SESSION['show'] .= $a->name."受了重伤，".$sk->name."威力大大增强。<br>";
			return 1.7;
		} elseif($a->hp <= round($a->mxhp * 0.1)) {
			$_SESSION['show'] .= $a->name."濒死，".$sk->name."威力达到极致。<br>";
			return 2;
		} else {
			return 1;
		}
	}

	// 输出CG函数
	public static function cgdisplay($name) {
		$filepath = "cg/$name";
		if(!file_exists($filepath)) {
			return 0;
		}
		if($_SERVER["SERVER_NAME"] == "bbs.fireemblem.net") {
			$localname="bbs.fireemblem.net";
		} else {
			$localname="res.fireemblem.net";
		}
		$_SESSION['show'] .= "<img src=".$filepath." alt=http://".$localname."/hhp4g/".$filepath."></img><p>";
	}

	/** 魂灭处理函数 **/
	public static function disillusion($t) {
		if($t->eventskill_status[20]->on == 1) {
			$_SESSION['show'] .= $t->name."恒河沙效果发动，魂灭被抵消<br>";
			$t->eventskill_status[20]->cancel($t);
			return 0;
		}
		if($t->defskill_status[6]->on == 1) {
			$_SESSION['show'] .= $t->name."受到二十八宿の守護，魂灭无效<br>";
			return 0;
		}
		$_SESSION['show'] .= $t->name."魂灭，幸存队友AP增加<br>";
		$t->hp = 0;
		$t->disillusion = 1;
		list($group,$key) = self::getitbyuuid($t->uuid);
		foreach($_SESSION[$group] as $sg) {
			if($sg->hp > 0 && !$sg->escaping) self::apointchange_2($sg,3);
		}
		return 1;
	}

	/** 根据uuid获取所有人阵营与下标 **/
	public static function getitbyuuid($uuid) {
		foreach($_SESSION['a'] as $k=>$v) {
			if($v->uuid == $uuid) return array('a',$k);
		}
		foreach($_SESSION['b'] as $k=>$v) {
			if($v->uuid == $uuid) return array('b',$k);
		}
		return false;
	}

	/** 命运组别特技天之裁定判定 **/
	public static function divine_judge($a,$b,$dmg) {
		if(self::ifgroupskill($a) == 10) {
			$tmp = mt_rand(1,100);
			$_SESSION['result'] .= "天の裁定乱数：$tmp<br>";
			if(25 >= $tmp) {
				$cutsp = round($dmg/5);
				$cutsp = ($b->sp - $cutsp < 0) ? $b->sp : $cutsp;
				$_SESSION['show'] .= $a->name."发动天の裁定，".$b->name."的SP流失".$cutsp."<br>";
				$b->sp -= $cutsp;
				return 1;
			}
		}
		return 0;
	}

	/** 正义组别特技天之盾判定，注意结算是在atkskill_class里 **/
	public static function divine_shield($b) {
		if(self::ifgroupskill($b) == 8 && $b->groupskcount < 1) {
			$tmp = mt_rand(1,100);
			$_SESSION['result'] .= "天の盾乱数：$tmp<br>";
			if(35 >= $tmp) {
				$_SESSION['show'] .= $b->name."发动天の盾，即死伤害被抵消！<br>";
				$b->groupskcount += 1;
				return 1;
			}
		}
		return 0;
	}

	/** 倒吊者组别技能异界住人判定 **/
	public static function hell_stayer($b) {
		if(self::ifgroupskill($b) == 12) {
			$tmp = mt_rand(1,100);
			$_SESSION['result'] .= "異界の住人乱数：$tmp<br>";
			if(25 >= $tmp) {
				$b->done = -1;
				$b->atkafter_status[4]->on = 1;
				$b->atkskill_status[52]->on = 1;
				$_SESSION['show'] .= $b->name."異界の住人发动，回光返照行动开始！<br>";
				return 1;
			}
		}
		return  0;
	}

	/** 永劫组别技能摂理の盾判定 **/
	public static function axiom_shield($b) {
		if(self::ifgroupskill($b) == 21 && $b->groupskcount == 0) {
			$_SESSION['show'] .= $b->name."摂理の盾发动！<br>";
			$b->before_status[44]->on = 1;
			$b->groupskcount += 1;
		}
	}

	/** 特殊台词处理函数 **/
	public static function spec_talk($sk,$id,$content) {
		require('config.php');
		$uuid = md5($sk->id.'+'.$sk->type.'+'.$sk->name.'+'.$id);
		$sql = "select id from spectalk where uuid='${uuid}'";
		$rows = $db->get_rows($sql);
		if($rows > 0) {
			$_SESSION['show'] .= "$content<br>";
		} else {
			$_SESSION['show'] .= "<font color=red>NEW SP TALK!</font><br>$content<br>";
			$_SESSION['extrainfo']['talkhistory'][] = array($sk->type,$sk->id,$uuid,$content,date('Y-m-d H:i:s')); 
		}
		return 1;
	}
	
	/** 裁判点结算 **/
	public static function judgepoint($a,$b,$method) {
		switch($method){
		case "DOWN":{$p = 3;break;}
		case "DAMAGE":{$p = 2;break;}
		case "KILL":{ 
			if($_SESSION['extrainfo']['ooduel'] == 1) break;
			if(isset($b->havedead)) {
				$p = 2;
			} else {
				$p = 5;
				$b->havedead = 1;
			}
			break;
			}
		case "ABN":{$p = 1;break;}
		case "DEF":{$p = -3;break;}
		case "DEFWEAK":{$p = -2;break;}
		case "VOID":{$p = -1;break;}
		case "CURE":{$p = -1;break;}
		}
		list($g,$pos) = self::getitbyuuid($a->uuid);
		$_SESSION['extrainfo'][$g.'_point'] += $p;
		return 1;
	}

	/** 是否觉醒 **/
	public static function is_arise($a) {
		if($a->hp > 0 && $a->before_status[17]->started == 1) return true;
		else return false;
	}

}

?>
